package com.example.demo.testOne;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;


/**
 * @description 时间单位
 *
 * @author lilinchun
 * @date 2023/08/01
 */
public class demo8 {


    public static void main(String[] args) {
        // 找出 2023-07 的开始时间与结束时间
        DateTime beginDate = DateUtil.beginOfMonth(DateUtil.parse("2023-07","yyyy-MM"));
        DateTime endDate = DateUtil.endOfMonth(DateUtil.parse("2023-07","yyyy-MM"));
        System.out.println(beginDate);
        System.out.println(endDate);


        ArrayList<String> day = getAllDateByParamDate(DateUtil.format(beginDate,"yyyy-MM-dd"), DateUtil.format(endDate,"yyyy-MM-dd"), "day");
        System.out.println(day);
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String dateStr = "2021-08-19 00:00:00";
        LocalDateTime date2 = LocalDateTime.parse(dateStr, fmt);
        DateTime parse = DateUtil.parse(dateStr, "yyyy-MM-dd");
        DateTime one = DateUtil.beginOfDay(parse);
        DateTime two = DateUtil.endOfDay(parse);
        System.out.println(one);
        System.out.println(two);

    }


    /**
     * 时间工具
     * @param startDate 开始日期
     * @param endDate 结束日期
     * @param type 类型：天-day 月-month 年-year
     * @return 时间区间
     */
    public static ArrayList<String> getAllDateByParamDate(String startDate, String endDate, String type) {
        if (!"year".equals(type) && !"month".equals(type) && !"day".equals(type)) {
            return null;
        }
        DateTime parseStartDate = DateUtil.parse(startDate);
        DateTime parseEndDate = DateUtil.parse(endDate);
        // 存储所有日期的list
        ArrayList<String> list = new ArrayList<>();
        // 获取所有年份
        if (type.equals("year")) {
            list.add(parseStartDate.toString("yyyy"));
            DateTime endDateOffset = parseEndDate.offsetNew(DateField.YEAR, -1);
            while (parseStartDate.isBefore(endDateOffset)) {
                DateTime stageDateTime = parseStartDate.offset(DateField.YEAR, 1);
                list.add(stageDateTime.toString("yyyy"));
                parseStartDate = stageDateTime;
            }
        } else if (type.equals("month")) {
            // 获取所有月份
            list.add(parseStartDate.toString("yyyy-MM"));
            DateTime endDateOffset = parseEndDate.offsetNew(DateField.MONTH, -1);
            while (parseStartDate.isBefore(endDateOffset)) {
                DateTime stageDateTime = parseStartDate.offset(DateField.MONTH, 1);
                list.add(stageDateTime.toString("yyyy-MM"));
                parseStartDate = stageDateTime;
            }
        } else {
            // 获取所有日期
            list.add(parseStartDate.toString("yyyy-MM-dd"));
            DateTime endDateOffset = parseEndDate.offsetNew(DateField.DAY_OF_MONTH, -1);
            while (parseStartDate.isBeforeOrEquals(endDateOffset)) {
                DateTime stageDateTime = parseStartDate.offset(DateField.DAY_OF_MONTH, 1);
                list.add(stageDateTime.toString("yyyy-MM-dd"));
                parseStartDate = stageDateTime;
            }
        }
        // 返回数据
        return list;
    }

}
